// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Page.java

package com.kaylyn.item;

import com.kaylyn.frame.EBookWindowContentPanel;
import com.kaylyn.util.Resource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

// Referenced classes of package com.kaylyn.item:
//            Top10, MovePage, Dialog, TogetherPhoto, 
//            ScrollText, Duck, PresentShow, ScrollText2, 
//            EBook

public class Page {

	public Page(EBook ebook, Point offscreenLocation,
			Dimension offscreenDimension) {
		components = new Vector();
		appletsNode = new Vector();
		buttonsNode = new Vector();
		this.ebook = ebook;
		this.offscreenDimension = offscreenDimension;
	}

	public void setNextPage(Page nextPage) {
		this.nextPage = nextPage;
	}

	public void setLastPage(Page lastPage) {
		this.lastPage = lastPage;
	}

	public BufferedImage snapshot() {
		return Resource.loadBufferedImage(image, offscreenDimension);
	}

	public Page getNextPage() {
		return nextPage;
	}

	public Page getLastPage() {
		return lastPage;
	}

	public void addImage(Node node) {
		NodeList list = node.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			String attribute = list.item(i).getNodeName();
			if (attribute.equals("src"))
				image = list.item(i).getFirstChild().getNodeValue().trim();
		}

	}

	public void addComponent(Node node) {
		appletsNode.add(node);
	}

	public void addButton(Node node) {
		buttonsNode.add(node);
	}

	public void getComponents(Point offscreenLocation) {
		getApplet(offscreenLocation);
		getButton(offscreenLocation);
	}

	public Iterator componentsIter() {
		return components.iterator();
	}

	private void getApplet(Point offscreenLocation) {
		Component c;
		for (Iterator iter = appletsNode.iterator(); iter.hasNext(); components
				.add(c)) {
			Node node = (Node) iter.next();
			NodeList applet = node.getChildNodes();
			String name = null;
			int x = 0;
			int y = 0;
			for (int i = 0; i < applet.getLength(); i++) {
				String attribute = applet.item(i).getNodeName();
				if (attribute.equals("name"))
					name = applet.item(i).getFirstChild().getNodeValue().trim();
				if (attribute.equals("x"))
					x = Integer.parseInt(applet.item(i).getFirstChild()
							.getNodeValue().trim());
				if (attribute.equals("y"))
					y = Integer.parseInt(applet.item(i).getFirstChild()
							.getNodeValue().trim());
			}

			x += offscreenLocation.x;
			y += offscreenLocation.y;
			c = null;
			if (name.equals("top10"))
				c = new Top10();
			else if (name.equals("movepage"))
				c = new MovePage();
			else if (name.equals("dialog"))
				c = new Dialog();
			else if (name.equals("togetherphoto"))
				c = new TogetherPhoto();
			else if (name.equals("scrolltext"))
				c = new ScrollText();
			else if (name.equals("duck"))
				c = new Duck();
			else if (name.equals("presentshow"))
				c = new PresentShow();
			else if (name.equals("scrolltext2"))
				c = new ScrollText2();
			else if (name.equals("timeline"))
				c = new TimeLine();
			else if (name.equals("contentpage"))
				c = new ContentPage();
			else if (name.equals("video"))
				c = new Video();
			c.setLocation(new Point(x, y));
		}

	}

	private void getButton(Point offscreenLocation) {
		final JButton button = new JButton();
		;
		for (Iterator iter = buttonsNode.iterator(); iter.hasNext(); components
				.add(button)) {
			Node node = (Node) iter.next();
			NodeList list = node.getChildNodes();
			int x = 0;
			int y = 0;
			int width = 0;
			int height = 0;
			String icon = null;
			String rolloverIcon = null;
			String link = null;
			for (int i = 0; i < list.getLength(); i++) {
				String attribute = list.item(i).getNodeName();
				if (attribute.equals("x"))
					x = Integer.parseInt(list.item(i).getFirstChild()
							.getNodeValue().trim());
				if (attribute.equals("y"))
					x = Integer.parseInt(list.item(i).getFirstChild()
							.getNodeValue().trim());
				if (attribute.equals("width"))
					width = Integer.parseInt(list.item(i).getFirstChild()
							.getNodeValue().trim());
				if (attribute.equals("height"))
					height = Integer.parseInt(list.item(i).getFirstChild()
							.getNodeValue().trim());
				if (attribute.equals("icon"))
					icon = list.item(i).getFirstChild().getNodeValue().trim();
				if (attribute.equals("rollovericon"))
					rolloverIcon = list.item(i).getFirstChild().getNodeValue()
							.trim();
				if (attribute.equals("link"))
					link = list.item(i).getFirstChild().getNodeValue().trim();
			}

			x += offscreenLocation.x;
			y += offscreenLocation.y;

			button.setFocusPainted(false);
			button.setBorderPainted(false);
			button.setContentAreaFilled(false);
			button.setCursor(Cursor.getPredefinedCursor(12));
			button.setLocation(new Point(x, y));
			button.setSize(new Dimension(width, height));
			if (icon != null)
				button.setIcon(new ImageIcon(Resource.loadImage(icon)));
			if (rolloverIcon != null)
				button.setRolloverIcon(new ImageIcon(Resource
						.loadImage(rolloverIcon)));
			final String l = link;
			button.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					((EBookWindowContentPanel) button.getParent()).pageLoading(
							Page.this, ebook.getPage(l));
					ebook.setCurrentPage(ebook.getPage(l));
				}

			});
		}

	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description.replace(' ', '\n').replace('.', ' ');
	}

	public void clearComponents() {
		components.clear();
	}

	private Page nextPage;
	private Page lastPage;
	private String image;
	private Vector components;
	private Vector appletsNode;
	private Vector buttonsNode;
	private EBook ebook;
	private Dimension offscreenDimension;
	private String description = "";
}
